package jaux.tank.generator.domain;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import jaux.tank.generator.util.NumberUtil;
import lombok.Data;
import lombok.experimental.FieldNameConstants;

@Data
@FieldNameConstants
public class TankEntityField {

	public static final String DEFAULT_FIELD_NAME = "newField";

	public static final Object[] COLUMNS = { "Name", "Type", "Enum", "Default Value", "Comment", "Required", "Unique", "Min", "Max",
		"Min Length", "Max Length", "Pattern", "Java Docs", "Annotations" };
	
	public enum FieldType{
		STRING, INTEGER, LONG, BIG_DECIMAL, FLOAT, DOUBLE, ENUM, BOOLEAN, 
		LOCAL_DATE, LOCAL_DATE_TIME, ZONED_DATE_TIME, INSTANT,
		UUID, BLOB, ANY_BLOB, IMAGE_BLOB, TEXT_BLOB
	}
	
	private List<String> javaDocs;
	
	private List<String> annotations;
	
	private String name;
	
	private String comment;
	
	private FieldType type;
	
	private String enumName;
	
	private FieldValidation validation;
	
	private String defaultValue;
	
	public TankEntityField() {
		this(DEFAULT_FIELD_NAME, FieldType.STRING);
		
	}
	public TankEntityField(String name, FieldType type) {
		this.name = name;
		this.type = type;
		this.javaDocs = new ArrayList<>();
		this.annotations = new ArrayList<>();
		this.validation = new FieldValidation();
		this.defaultValue = "";
		this.comment = "";
		
	}
	
	public Object[] toRowData() {
		return new Object[] { this.getName(), this.getType(), this.getEnumName(),
				this.getDefaultValue(), this.getComment(),
				this.getValidation().isRequired(), this.getValidation().isUnique(),
				this.getValidation().getMin(), this.getValidation().getMax(),
				this.getValidation().getMinLength(), this.getValidation().getMaxLength(),
				this.getValidation().getPattern(),
				this.getJavaDocs() != null ? String.join("\n", this.getJavaDocs()) : "",
				this.getAnnotations() != null ? String.join("\n", this.getAnnotations()) : "" };
	}
	public static TankEntityField fromRowData(Object[] rowData) {
		TankEntityField field = new TankEntityField();
		field.setName((String) rowData[0]);
		field.setType((TankEntityField.FieldType) rowData[1]);
		field.setEnumName((String)rowData[2]);
		field.setDefaultValue((String) rowData[3]);
		field.setComment((String) rowData[4]);
		field.setValidation(new TankEntityField.FieldValidation());
		field.getValidation().setRequired((Boolean) rowData[5]);
		field.getValidation().setUnique((Boolean) rowData[6]);
		field.getValidation().setMin(NumberUtil.getDouble(rowData[7]));
		field.getValidation().setMax(NumberUtil.getDouble(rowData[8]));
		field.getValidation().setMinLength(NumberUtil.getInteger(rowData[9]));
		field.getValidation().setMaxLength(NumberUtil.getInteger(rowData[10]));
		field.getValidation().setPattern((String) rowData[11]);
		String javaDocsValue = (String) rowData[12];
		if (javaDocsValue != null && !StringUtils.isBlank(javaDocsValue)) {
			field.setJavaDocs(Arrays.asList(StringUtils.strip(javaDocsValue).split("\n")));

		}
		String annotationsValue = (String) rowData[13];
		if (annotationsValue != null && !StringUtils.isBlank(annotationsValue)) {

			field.setAnnotations(Arrays.asList(StringUtils.strip(annotationsValue).split("\n")));
		}
		return field;
	}
	@Data
	public static class FieldValidation{
		
		private boolean required;
		private boolean unique;
		
		private Double min;
		private Double max;
		
		private int minLength;
		private int maxLength=255;
		
		private String pattern;
		
	}
}
